package com.dkd.manage.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.sql.ast.expr.SQLCaseExpr;
import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.dkd.common.annotation.Log;
import com.dkd.common.core.controller.BaseController;
import com.dkd.common.core.domain.AjaxResult;
import com.dkd.common.enums.BusinessType;
import com.dkd.manage.domain.Sku;
import com.dkd.manage.service.ISkuService;
import com.dkd.common.utils.poi.ExcelUtil;
import com.dkd.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品管理相关接口
 * 
 * @author fuyuncheng
 * @date 2025-05-07
 */
@Api(value = "/manage/sku", tags = {"商品管理相关接口"})
@RestController
@RequestMapping("/manage/sku")
public class SkuController extends BaseController
{
    @Autowired
    private ISkuService skuService;

    /**
     * 查询商品管理列表
     */
    @ApiOperation(value = "查询商品管理列表", notes = "查询商品管理列表", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('manage:sku:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sku sku)
    {
        startPage();
        List<Sku> list = skuService.selectSkuList(sku);
        return getDataTable(list);
    }

    /**
     * 导出商品管理列表
     */
    @ApiOperation(value = "导出商品管理列表", notes = "导出商品管理列表", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('manage:sku:export')")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sku sku)
    {
        List<Sku> list = skuService.selectSkuList(sku);
        ExcelUtil<Sku> util = new ExcelUtil<Sku>(Sku.class);
        util.exportEasyExcel(response, list, "商品管理数据");
    }
    /**
     * 导入商品信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "file", name = "file", value = "", required = true)
    })
    @ApiOperation(value = "导入商品信息", notes = "导入商品信息", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('manage:sku:add')")
    @Log(title = "商品管理", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file)
    {
        try {
            ExcelUtil<Sku> util= new ExcelUtil<Sku>(Sku.class);
            List<Sku> skus = util.importExcel(file.getInputStream());
            return toAjax(skuService.insertSkuList(skus));
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("导入失败");
        }
    }
    /**
     * 获取商品管理详细信息
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "skuId", value = "", required = true)
    })
    @ApiOperation(value = "获取商品管理详细信息", notes = "获取商品管理详细信息", httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('manage:sku:query')")
    @GetMapping(value = "/{skuId}")
    public AjaxResult getInfo(@PathVariable("skuId") Long skuId)
    {
        return success(skuService.selectSkuBySkuId(skuId));
    }

    /**
     * 新增商品管理
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Sku", name = "sku", value = "", required = true)
    })
    @ApiOperation(value = "新增商品管理", notes = "新增商品管理", httpMethod = "POST")
    @PreAuthorize("@ss.hasPermi('manage:sku:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sku sku)
    {
        return toAjax(skuService.insertSku(sku));
    }

    /**
     * 修改商品管理
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Sku", name = "sku", value = "", required = true)
    })
    @ApiOperation(value = "修改商品管理", notes = "修改商品管理", httpMethod = "PUT")
    @PreAuthorize("@ss.hasPermi('manage:sku:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sku sku)
    {
        return toAjax(skuService.updateSku(sku));
    }

    /**
     * 删除商品管理
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "Long[]", name = "skuIds", value = "", required = true)
    })
    @ApiOperation(value = "删除商品管理", notes = "删除商品管理", httpMethod = "DELETE")
    @PreAuthorize("@ss.hasPermi('manage:sku:remove')")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{skuIds}")
    public AjaxResult remove(@PathVariable Long[] skuIds)
    {
        int row = skuService.deleteSkuBySkuIds(skuIds);
        if(row>0){
            return AjaxResult.success();
        }else{
            return AjaxResult.error("该该商品与货道关联，无法删除");
        }
    }
}
